package com.ruoyi.bnt.controller;

import java.util.List;

import com.ruoyi.common.utils.uuid.UUID;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bnt.domain.BntCourse;
import com.ruoyi.bnt.service.IBntCourseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author yyj
 * @date 2020-10-03
 */
@Controller
@RequestMapping("/bnt/course")
public class BntCourseController extends BaseController
{
    private String prefix = "bnt/course";

    @Autowired
    private IBntCourseService bntCourseService;

    @RequiresPermissions("bnt:course:view")
    @GetMapping()
    public String course()
    {
        return prefix + "/course";
    }

    /**
     * 查询课程列表
     */
    @RequiresPermissions("bnt:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BntCourse bntCourse)
    {
        startPage();
        List<BntCourse> list = bntCourseService.selectBntCourseList(bntCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @RequiresPermissions("bnt:course:export")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BntCourse bntCourse)
    {
        List<BntCourse> list = bntCourseService.selectBntCourseList(bntCourse);
        ExcelUtil<BntCourse> util = new ExcelUtil<BntCourse>(BntCourse.class);
        return util.exportExcel(list, "course");
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存课程
     */
    @RequiresPermissions("bnt:course:add")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BntCourse bntCourse)
    {
        if(bntCourse.getId() == null || bntCourse.getId().isEmpty())
            bntCourse.setId(UUID.randomUUID().toString());
        return toAjax(bntCourseService.insertBntCourse(bntCourse));
    }

    /**
     * 修改课程
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        BntCourse bntCourse = bntCourseService.selectBntCourseById(id);
        mmap.put("bntCourse", bntCourse);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @RequiresPermissions("bnt:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BntCourse bntCourse)
    {
        return toAjax(bntCourseService.updateBntCourse(bntCourse));
    }

    /**
     * 删除课程
     */
    @RequiresPermissions("bnt:course:remove")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bntCourseService.deleteBntCourseByIds(ids));
    }

    @RequiresPermissions("bnt:course:add")
    @GetMapping("/article/{infoId}")
    public String articleInfoId(@PathVariable("infoId") String infoId, ModelMap mmap)
    {
        mmap.put("infoId", infoId);
        return "bnt/course/articleinfo/articleinfo";
    }

    @RequiresPermissions("bnt:course:add")
    @GetMapping("/video/{infoId}")
    public String videoInfoId(@PathVariable("infoId") String infoId, ModelMap mmap)
    {
        mmap.put("infoId", infoId);
        return "bnt/course/courseissue/courseissue";
    }
}
